import { NetEnumDef } from "@/netBase/NetEnumDef";
import { NetPacket } from "@/netBase/NetPacket";
import { getDeviceId } from "@/net/Utils";
// import { Net } from "@/net/Net";
import H5Websocket from "@/net/Net1";
import { IP } from "./useStoreMethods";
import { Local } from "./storage";
import { MessageEvent2 } from "@/net/MessageEvent2";
import { NetMsgType } from "@/netBase/NetMsgType";
// import { showToast } from '@nutui/nutui'
const onHander_check_version = async (message: any) => {
    if (message.result != NetEnumDef.check_version_result.cvr_yes) {
        console.log("check version failed");
        return;
    }
    // 未登录汇率
    // MessageEvent2.addMsgEvent(
    //     NetMsgType.msgType.msg_notify_get_currency_rate,
    //     handleCurrencyRate
    // );
    // const currencyRate = NetPacket.req_get_currency_rate();
    // H5Websocket.sendMessageHandler(currencyRate);
    needLoginApi();
};
export const needLoginApi = async (Reconnect = false) => {
    // 	// 继续登录服务器
    console.error('继续', Local.get('user')?.token)
    if (Local.get('user')?.token) {
        let tb_req = NetPacket.req_role_login_with_ip();
        tb_req.uid = String(Local.get('user').user_id);
        tb_req.server_id = 2;
        tb_req.token = Local.get('user').token;
        tb_req.type = NetEnumDef.connect_type.re_connect;
        tb_req.version = NetEnumDef.get_proto_version();
        tb_req.device_id = await getDeviceId();
        tb_req.ip = await IP();
        // Net.instance.sendRequest(tb_req);
        H5Websocket.sendMessageHandler(tb_req);
        // 获取所有收藏id集合
        const query = NetPacket.req_all_collected()
        query.agentId = -1
        H5Websocket.sendMessageHandler(query);
        // 目前观察好像没有什么用，先去掉
        // let req_roleinfo_with_id = NetPacket.req_roleinfo_with_id();
        // req_roleinfo_with_id.id = Local.get('user').user_id;
        // // Net.instance.sendRequest(req_roleinfo_with_id);
        // H5Websocket.sendMessageHandler(req_roleinfo_with_id);
        if (!Reconnect) {
            let req_user_info = NetPacket.req_user_info();
            // Net.instance.sendRequest(req_user_info);
            H5Websocket.sendMessageHandler(req_user_info);
            let vipInfoReq = NetPacket.req_vip_info();
            // Net.instance.sendRequest(vipInfoReq);
            H5Websocket.sendMessageHandler(vipInfoReq);
        }
    }
}
// 添加取消收藏
export const reqCollect = async (game: any, allCollected: any) => {
    const query = NetPacket.req_modify_collect()
    query.agentId = game.agentId || game.id  //id 为厂商游戏  例如 体育 真人之类
    query.id = game.id
    query.type = allCollected.includes(query.id) ? 1 : 0
    Local.set('gameId', query.id)
    H5Websocket.sendMessageHandler(query);
}
// 请求第三方游戏url
export const getGameUrl = async (item: any, lang: string, Page: any) => {
    Page.setGameIsLoading(true)
    Local.set('direction', item.direction || 0)
    let tb = NetPacket.req_3rd_game_login();
    tb.agentId = item.agentId || item.id //platform.three_game_kind_id;
    tb.gameId = item.gameId //platform.three_platform_id;
    tb.kindId = item.kindId //|| item.venue_id //platform.venue_id;
    tb.lang = lang;
    tb.device_type = 2  //2 为手机浏览器打开  后期使用此方法 getBlientType
    H5Websocket.sendMessageHandler(tb);
}
export const checkVersion = () => {
    MessageEvent2.addMsgEvent(
        NetMsgType.msgType.msg_notify_check_version,
        onHander_check_version
    );
    let req_check_version_req = NetPacket.req_check_version();
    req_check_version_req.version = 1;
    H5Websocket.sendMessageHandler(req_check_version_req);

}